#include "stdafx.h"
#include "Slave.h"
/*Slave::Slave()
{
	m_nSlaveId = 0;
	m_pSlaveCSVInfo = NULL;
	LOGGER_INFO("Slave:%p,m_nSlaveId:%d",this,m_nSlaveId);
}*/

Slave::Slave(SlaveId nSlaveId)
{
	m_nSlaveId = nSlaveId;
	m_bWatch = false;
	m_pOwnerPlayer = NULL;
	LOGGER_INFO("Slave(SlaveId nSlaveId):%p,m_nSlaveId:%d",this,m_nSlaveId);
}
Slave::~Slave()
{
	LOGGER_INFO("~Slave:%p,m_nSlaveId:%d",this,m_nSlaveId);
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		singleton<SkillManager>::instance().Remove(m_arSkills[i]);
		LOGGER_INFO("~Slave:%p,m_arSkills:%d",this,m_arSkills[i]);
	}
}
void Slave::SetOwnerPlayer(Player* pPlayer)
{
	m_pOwnerPlayer = (void*)pPlayer;
}
Player* Slave::GetOwnerPlayer()
{ 
	return (Player*)m_pOwnerPlayer;
};
void Slave::addSkill(Int32 csv_id)
{
	if (m_arSkills.size() >= MAX_SKILLS_SIZE)
	{
		LOGGER_ERROR("addSkill,m_arSkills.size() >= SlaveInfo::MAX_SKILLS_SIZE");
		return;
	}

	Skill *pSkill = singleton<SkillManager>::instance().CreateFromCSVId(csv_id);
	if (pSkill == NULL)
	{
		LOGGER_ERROR("addSkill,pSkill == NULL,csv_id:%d",csv_id);
		return;
	}
	m_arSkills.push_back(pSkill->GetSkillId());
	LOGGER_INFO("addSkill,pSkill->GetSkillId():%d",pSkill->GetSkillId());
}
void Slave::removeSkill(Int32 csv_id)
{
	std::vector<SkillId>::iterator iter = m_arSkills.begin();
	std::vector<SkillId>::iterator iterend = m_arSkills.end();
	for (; iter != iterend; iter++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(*iter);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
			{
				singleton<SkillManager>::instance().Remove(*iter);
				m_arSkills.erase(iter);
				return;
			}
		}
	}
}
bool Slave::hasSkill(Int32 csv_id)
{
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
				return true;
		}
	}
	return false;
}
Skill* Slave::getSkill(Int32 csv_id)
{
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
				return pSkill;
		}
	}
	return NULL;
}
void Slave::decodeSkills()
{
	if (m_stSlaveInfo.m_nSkillsSize > MAX_SKILLS_SIZE)
	{
		LOGGER_ERROR("decodeSkills,m_stSlaveInfo.m_nSkillsSize > SlaveInfo::MAX_SKILLS_SIZE");
		return;
	}
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		singleton<SkillManager>::instance().Remove(m_arSkills[i]);
	}
	m_arSkills.clear();
	Skill *pSkill = NULL;
	for (UInt32 i = 0; i < m_stSlaveInfo.m_nSkillsSize; i++)
	{
		if (m_stSlaveInfo.m_arSkills[i].csv_id != 0 )
		{
			pSkill = singleton<SkillManager>::instance().CreateFromSkillInfo(m_stSlaveInfo.m_arSkills[i]);
			if (pSkill == NULL)
			{
				LOGGER_ERROR("decodeSkills,m_stSlaveInfo.m_arSkills[i].csv_id != 0,but pSkill == NULL,csv_id:%d",m_stSlaveInfo.m_arSkills[i].csv_id);
			}
			else
			{
				m_arSkills.push_back(pSkill->GetSkillId());
				LOGGER_INFO("decodeSkills,pSkill->GetSkillId():%d",pSkill->GetSkillId());
			}
		}
	}
	if (hasSkill(SKILL_ID_TP)) removeSkill(SKILL_ID_TP);
}
void Slave::encodeSkills()
{
	if (m_arSkills.size() > MAX_SKILLS_SIZE)
	{
		LOGGER_ERROR("encodeSkills,m_arSkills.size() >= SlaveInfo::MAX_SKILLS_SIZE");
		return;
	}

	m_stSlaveInfo.m_nSkillsSize = m_arSkills.size();
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			m_stSlaveInfo.m_arSkills[i] = pSkill->GetSkillInfo();
		}
	}
}
void Slave::sendSkills(PacketEncoder &encoder)
{
	encoder.AddItem(Int32(m_arSkills.size()));
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill == NULL) continue;

		encoder.AddItem(pSkill->GetSkillInfo().csv_id);
		encoder.AddItem(pSkill->GetSkillInfo().level);
	}
}